<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;

class ELEVENRECORD extends NotORM
{

    protected function getTableName($id)
    {
        return 'eleven_record';
    }

    public function getPercent($id)
    {
        $data = $this->getORM()->where(['activity_id' => $id])->sum('num');
        if(!empty($data))
            return $data;
        return 0;
    }

    public function getCount()
    {
        $sql =  "select "
                ."count(*) as total "
                ."from (select count(*) from byb_eleven_record group by uid) u";
        $data = $this->getORM()->queryRows($sql);
        if(!empty($data))
            return $data[0]['total'];
        return 0;
    }

    public function findByIco($uid,$activity_id)
    {
        $data = $this->getORM()->where(['activity_id' => $activity_id])->where(['uid' => $uid])->fetchOne();
        if(!empty($data))
            return $data;
        return 0;
    }

    public function findByUid($uid)
    {
        $sql =  "select a.id, a.ico, a.icon, a.price, a.unit, a.num, a.start_time, a.end_time "
            .   "from byb_eleven_record r left join byb_eleven_activity a on r.activity_id = a.id "
            .   "where r.uid = $uid and a.level = 0";
        $data = $this->getORM()->queryRows($sql);
        return $data;
    }

    public function getActivity($uid)
    {
        $data = $this->getORM()->where(['uid' => $uid])->where('status in (1,2)')->select('activity_id')->fetchAll();
        return $data;
    }
}